﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using DataObjectsPhone;
using ee_wp_tourmaker.WebServices;
using ee_wp_tourmaker;
using System.Windows.Navigation;
using System.Collections.ObjectModel;
using System.Windows.Media.Imaging;

namespace ee_wp_tourmaker.Views
{
    public partial class NewChallengePage : PhoneApplicationPage
    {
        public NewChallengePage()
        {
            InitializeComponent();
            if (App.MainModel.newChallenge == null)
            {
                App.MainModel.newChallenge = new Challenge();
                App.MainModel.newChallenge.checkpointsList = new ObservableCollection<CheckPoint>();
            }
            checkpointListBox.ItemsSource = App.MainModel.newChallenge.checkpointsList;
            


            
        }

        private void newCheckPointButton_Click(object sender, RoutedEventArgs e)
        {
            App.MainModel.SelectedCheckPoint.Latitude = (Decimal)0.0;
            App.MainModel.SelectedCheckPoint.Longitude = (Decimal)0.0;



            NavigationService.Navigate(new Uri("/Views/NewCheckPointPage.xaml", UriKind.RelativeOrAbsolute));
        }

        private void newCheckPointTextBlock_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {

            App.MainModel.SelectedCheckPoint.Latitude = (Decimal)0.0;
            App.MainModel.SelectedCheckPoint.Longitude = (Decimal)0.0;
            NavigationService.Navigate(new Uri("/Views/NewCheckPointPage.xaml", UriKind.RelativeOrAbsolute));
        }

        private void ApplicationBarSaveButton_Click(object sender, EventArgs e)
        {
            App.MainModel.newChallenge.Name = nameTextBox.Text;
            App.MainModel.newChallenge.Description = descTextBox.Text;

            if (App.MainModel.newChallenge.Name.Length > 0 &&
                App.MainModel.newChallenge.checkpointsList.Count > 0)
            {

                uploadChallengeToServer(App.MainModel.newChallenge);
                NavigationService.GoBack();
            }
            else
            {
                MessageBox.Show("You must add atleast one checkpoint a point on the map and name of your challenge!");
            }

            
        }

        private void uploadChallengeToServer(Challenge challenge)
        {
            List<AzureService.AddCheckPoint> ps = new List<AzureService.AddCheckPoint>();
            foreach (var item in challenge.checkpointsList)
            {
                AzureService.AddCheckPoint p = new AzureService.AddCheckPoint();
                p.Accuracy = item.Accuracy;
                p.Address = item.Address;
                p.Description = item.Description;
                p.Id = Guid.NewGuid();
                p.Latitude = item.Latitude;
                p.Longitude = item.Longitude;
                p.Name = item.Name;
                p.OtherLink = item.OtherLink;
                p.Picture = new byte[0];
                p.Points = item.Points;
                p.WikiLink = item.WikiLink;
                ps.Add(p);
            }
	

            App.MainModel.Changes.Items.Add(new AzureService.SyncItem()
            {
                

                ItemType = AzureService.SyncItemType.AddChallenge,
                NewChallenge = new AzureService.AddChallenge()
                {
                    Id = Guid.NewGuid(),
                    Name = challenge.Name,
                    Description = challenge.Description,
                    CheckPoints = ps,                    
                    
                }
            });

            SaveChangesService service = new SaveChangesService();
            service.SaveChangesStart();
        }

        private void ApplicationBarCancelButton_Click(object sender, EventArgs e)
        {
            App.MainModel.newChallenge = null;
            NavigationService.GoBack();
        }

        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            // do some stuff ...
            App.MainModel.newChallenge = null;
            
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            pointsTextBlock.Text = "Points: "+App.MainModel.newChallenge.checkpointsList.Count * 2;

            Visibility darkBackgroundVisibility =
          (Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"];
            if (darkBackgroundVisibility == Visibility.Visible)
            {
                var brush = new ImageBrush();
                brush.ImageSource = new BitmapImage(new Uri("../Icons/appbar.new.rest.png", UriKind.Relative));
                newCheckPointButton.Background = brush;
            }
            else
            {
                var brush = new ImageBrush();
                brush.ImageSource = new BitmapImage(new Uri("../Icons/appbar.add.rest.png", UriKind.Relative));
                newCheckPointButton.Background = brush;
            }
        }

        private void MenuItemDelete_Click(object sender, RoutedEventArgs e)
        {
            ListBoxItem selectedListBoxItem = checkpointListBox.ItemContainerGenerator.ContainerFromItem((sender as MenuItem).DataContext) as ListBoxItem;
            CheckPoint checkpoint = selectedListBoxItem.DataContext as CheckPoint;
            try
            {
                App.MainModel.newChallenge.checkpointsList.Remove(checkpoint);
                pointsTextBlock.Text = "Points: " + App.MainModel.newChallenge.checkpointsList.Count * 2;
            }
            catch (Exception)
            {

            }
            

        }


    }
}